<template>
	<view class="uni-numbox x-f" @tap.stop>
		<view @tap.stop="_calcValue('minus')" class="uni-numbox__minus">
			<button class=" cu-btn uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue <= min || disabled }"><image class="icon-img" :src="Url+'index/ic-jj.png'"></image></button>
		</view>
		<input :disabled="true" @blur="_onBlur" class="uni-numbox__value" type="number" v-model="inputValue" />
		<view @tap.stop="_calcValue('plus')" class="uni-numbox__plus">
			<button class=" cu-btn uni-numbox--text" :class="{ 'uni-numbox--disabled': inputValue >= max || disabled }"><image class="icon-img" :src="Url+'index/ic-add.png'"></image></button>
		</view>
	</view>
</template>
<script>
export default {
	name: 'UniNumberBox',
	props: {
		value: {
			type: Number,
			default: 1
		},
		min: {
			type: Number,
			default: 1
		},
		max: {
			type: Number,
			default: 9999
		},
		step: {
			type: Number,
			default: 1
		},
		disabled: {
			type: Boolean,
			default: false
		},
		currentSkuPrice: {
			type: Object,
			default: () => {return {}}
		},
		goodsInfo: {
			type: Object,
			default: () => {return {}}
		}
	},
	computed: {
	
	},
	data() {
		return {
			inputValue: 0,
			Url:this.Imgurl,
			type:'ad',
		};
	},
	watch: {
		value(val) {
			this.inputValue = +val;
		},
		inputValue(newVal, oldVal) {
			if (+newVal !== +oldVal) {
				this.$emit('change', newVal);
			}
		},
		currentSkuPrice(val) {
			let that = this;
			if(JSON.stringify(val) !== '{}') {
				if(that.inputValue >= val.stock) {
					that.inputValue = val.stock;
				}
			}
		}
	},
	created() {
		this.inputValue = +this.value;
	},
	methods: {
		_calcValue(type) {
			this.type = type;
			let that = this;
			if (this.disabled) {
				return;
			}
			const scale = this._getDecimalScale();
			let value = this.inputValue * scale;
			let step = this.step * scale;
			if (type === 'minus') {
				value -= step;
				if (value < this.min) {
					return;
				}
				if (value > this.max) {
					value = this.max;
				}
			} else if (type === 'plus') {
				if(JSON.stringify(that.currentSkuPrice) !== '{}') {
					if(value >= that.currentSkuPrice.stock) {
						that.$tools.toast('库存不足');
						return;
					}
				}
				
				if(that.goodsInfo.activity_type === 'seckill') {
					let rules = that.goodsInfo.activity.rules;
					if(rules.limit_buy != 0 && value >= rules.limit_buy) {
						that.$tools.toast('本次活动最多购买'+ rules.limit_buy + '件');
						return;
						
					}
				}
				value += step;
				if (value > this.max) {
					return;
				}
				if (value < this.min) {
					value = this.min;
				}
			}

			this.inputValue = String(value / scale);
		},
		_getDecimalScale() {
			let scale = 1;
			// 浮点型
			if (~~this.step !== this.step) {
				scale = Math.pow(10, (this.step + '').split('.')[1].length);
			}
			return scale;
		},
		_onBlur(event) {
			let value = event.detail.value;
			if (!value) {
				// this.inputValue = 0;
				return;
			}
			value = +value;
			if (value > this.max) {
				value = this.max;
			} else if (value < this.min) {
				value = this.min;
			}
			this.inputValue = value;
		}
	}
};
</script>
<style lang="scss" scoped>
	.iconj{
		font-size:40upx;
		color: #999999;
	}
	.icon-img{
		width: 40upx;
		height: 40upx;
		border-radius: 50%;
	}
	.icona{
		font-size: 40upx;
		color: #f73e2f;
	}
	/* x水平排列*/
	.x-f {
		display: flex;
		align-items: center;
	}
	/*x两端且水平居中*/
	.x-bc {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	/*x平分且水平居中*/
	.x-ac {
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
	/*x水平靠上对齐*/
	.x-start {
		display: flex;
		align-items: flex-start;
	}
	/*x水平靠下对齐*/
	.x-end {
		display: flex;
		align-items: flex-end;
	}
	/*上下左右居中*/
	.x-c {
		display: flex;
		justify-content: center;
		align-items: center;
	}
	/*y竖直靠左*/
	.y-start {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
	}
	/*y竖直靠右*/
	.y-end {
		display: flex;
		flex-direction: column;
		align-items: flex-end;
	}
	/*y竖直居中*/
	.y-f {
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	// y竖直两端
	.y-b {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}
	/*y竖直两端居中*/
	.y-bc {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
	}
	
$box-height: 35px;
/* #ifdef APP-NVUE */
$box-line-height: 35px;
/* #endif */
$box-line-height: 26px;
$box-width: 35px;
.iconfont {
	font-size: 50rpx;
}
.uni-numbox {
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	flex-direction: row;
	height: $box-height;
	line-height: $box-height;
	.cu-btn {
		background: none;
		padding: 0;
		&::after{
			border: none;
		}
	}
}

.uni-numbox__value {
	background-color: none;
	width: 40px;
	height: $box-height;
	text-align: center;
	font-size: 28rpx;
}

.uni-numbox__minus {
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	flex-direction: row;
	align-items: center;
	justify-content: center;
	.icon-reduce-fill {
		color: #e7e7e7;
	}
}

.uni-numbox__plus {
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	flex-direction: row;
	align-items: center;
	justify-content: center;
	.icon-add-fill {
		color: #e9b564;
	}
}

.uni-numbox--text {
	font-size: 32rpx;
	line-height: 40rpx;
	color: #fff;
}

.uni-numbox--disabled {
	color: #fff;
}
</style>
